/**
 * 
 */
package GUI.PlayerPanel;

/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
import javax.swing.*;
import Utils.Utils;
import Player.Player;

@SuppressWarnings("serial")
public class PlayerButton extends JButton {

    Integer placing_number;//index from player in order to place on panel

    public PlayerButton(final Player player) {
	// These statements enlarge the button so that it
	// becomes a circle rather than an oval.
	placing_number = player.getNumber();
	Dimension size = getPreferredSize();
	size.width = 10;
	size.height = 10;
	// Set the Preferred Size
	setPreferredSize(size);
	//Set the Minimum Size
	setMinimumSize(size);
	// Double the Size
	size.width = size.width * 2;
	size.height = size.height * 2;
	// Set the Maximum Size
	setMaximumSize(size);
	//set color as player color
	this.setBackground(player.getColor());
	// set the Player Name as the Tool tip
	this.setToolTipText(player.getName());
	// Set Button Properties
	this.setOpaque(false);
	this.setFocusPainted(false);
	this.setBorderPainted(false);
	this.setContentAreaFilled(false);
	this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
	// This call causes the JButton not to paint
	// the background.
	// This allows us to paint a round background.
	setContentAreaFilled(false);

	// Add an Action to this Button
	addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent e) {
		Utils.writeText(player.toString());

	    }
	});
	// Repaint and Validate
	repaint();
	validate();
    }

    public Integer getPlacing_number() {
	return placing_number;
    }

    public void delete() {
	this.setVisible(false);
    }

    // Paint the round background and label.
    @Override
    protected void paintComponent(Graphics g) {
	if (getModel().isArmed()) {
	    // You might want to make the highlight color
	    // a property of the PlayerButton class.
	    g.setColor(getBackground().darker());
	} else {
	    g.setColor(getBackground());
	}
	g.fillOval(0, 0, getSize().width - 1,
		getSize().height - 1);

	// This call will paint the label and the
	// focus rectangle.
	super.paintComponent(g);
    }

    // Paint the border of the button using a simple stroke.
    @Override
    protected void paintBorder(Graphics g) {
	g.setColor(getForeground());
	g.drawOval(0, 0, getSize().width - 1,
		getSize().height - 1);
    }
    // Hit detection.
    Shape shape;

    public boolean contains(int x, int y) {
	// If the button has changed size,
	// make a new shape object.
	if (shape == null
		|| !shape.getBounds().equals(getBounds())) {
	    shape = new Ellipse2D.Float(0, 0,
		    getWidth(), getHeight());
	}
	return shape.contains(x, y);
    }

    /**
     * @return The Player Buttons' Color
     */
    public Color getColor() {
	// Return the The Player Color
	return getBackground();
    }
}
